from nodetypes.formats.rdf.Events import EventHandler
from nodetypes.services.search.UrlService import Service as SearchService
from nodetypes.services.TextSearchConfig import Serializer as ConfigSerializer

from documents.SearchResults import Document


class EventHandler(EventHandler):
    
    newSearchService        = SearchService
    newConfigSerializer     = ConfigSerializer
    
    newDocument = Document
        
    def process(self, context, source):
        eventData = context.event.data            
        eventData.config = self.newConfigSerializer().deserialize(source.asInputSource())
        if self.newSearchService().execute(context, source):
            return self.newDocument().createDocument(
                context,
                source
            )
        else:
            from myelin.server.lib.http.Exceptions import Exception 
            from myelin.server.lib.http.Exceptions import Error
            raise Exception(
                Error.BAD_REQUEST_PARAMS,
                eventData = context.event.data
            )
        
del Document, \
    SearchService